using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Snippets
/// </summary>
public class Snippets
{
    private Authentication auth;

	public Snippets()
	{
        auth = new Authentication();
	}

    /// <summary>
    /// Returns the text of a snippet given a title, or null if no snippet was found with that given title
    /// </summary>
    /// <param name="title">title of the snippet</param>
    /// <param name="token">security token</param>
    /// <returns>text of snippet as a string, null if none exist or if the token is invalid</returns>
    public string GetSnippet(string title, string token)
    {
        if (auth.ValidateToken(token))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSModerators"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT Subject FROM Snippets WHERE (Title = '" + title + "')", connection);
            SqlDataReader reader = command.ExecuteReader();
            if(!reader.Read())
                return null;
            else
                return reader.GetString(0);
        }
        else
            return null;
    }

    /// <summary>
    /// Returns a list of the titles of available snippets
    /// </summary>
    /// <param name="token">security token</param>
    /// <returns>list of snippets as a string array, null if token is invalid</returns>
    public string[] GetSnippetList(string token)
    {
        if (auth.ValidateToken(token))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSModerators"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("SELECT Title FROM Snippets", connection);
            SqlDataReader reader = command.ExecuteReader();
            System.Collections.Generic.List<string> snippetList = new System.Collections.Generic.List<string>();
            int i = 0;
            while (reader.Read())
            {
                snippetList.Add(reader.GetString(i));
            }
            return snippetList.ToArray();
        }
        else
            return null;
    }

    /// <summary>
    /// Edits the given snippet title
    /// </summary>
    /// <param name="title">title of the snippet we want to edit</param>
    /// <param name="newSubject">new subject that has been edited</param>
    /// <param name="token">security token</param>
    public bool EditSnippet(string title, string newSubject, string token)
    {
        if (auth.ValidateToken(token))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSModerators"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("UPDATE Snippets SET Subject = '" + newSubject + "' WHERE Title = '" + title + "'", connection);
            command.ExecuteNonQuery();
            return true;
        }
        else
            return false;
    }

    public bool AddSnippet(string title, string subject, string token)
    {
        if (auth.ValidateToken(token))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSModerators"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("INSERT INTO Snippets VALUES ('" + title + "','" + subject + "')", connection);
            command.ExecuteNonQuery();
            return true;
        }

        else
            return false;
    }

    public bool DeleteSnippet(string title, string token)
    {
        if (auth.ValidateToken(token))
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MSModerators"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("DELETE FROM Snippets WHERE Title = '" + title + "'", connection);
            command.ExecuteNonQuery();
            return true;
        }
        else
            return false;
    }
}
